package com.zzsz.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zzsz.domain.RecipientQueryDTO;
import com.zzsz.domain.WaringRecipient;
import com.zzsz.mapper.RecipientMapper;
import com.zzsz.service.RecipientService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * --- 这有何难 ---
 * ƪ(˘⌣˘)ʃ
 *
 * @author 超级无敌张恒阳大王
 * @date 4/9/2025
 * @desc 预警接收人serviceImpl
 */
@Slf4j
@Service
public class RecipientServiceImpl implements RecipientService {
	@Autowired
	private RecipientMapper recipientMapper;

	@Override
	public PageInfo<WaringRecipient> getRecipientList(RecipientQueryDTO recipientQueryDTO) {
		PageHelper.startPage(recipientQueryDTO.getPageNum(), recipientQueryDTO.getPageSize());

		List<WaringRecipient> recipientList = recipientMapper.selectAll(recipientQueryDTO);
		log.debug("ServiceImpl层 recipientList:{}", recipientList);
		return new PageInfo<>(recipientList);
	}

	@Override
	public int addRecipient(WaringRecipient waringRecipient) {
		log.info("添加预警接收人: {}", waringRecipient);
		return recipientMapper.insert(waringRecipient);
	}

	@Override
	public int updateRecipient(WaringRecipient waringRecipient) {
		log.info("更新预警接收人: {}", waringRecipient);
		return recipientMapper.update(waringRecipient);
	}

	@Override
	public int deleteRecipient(Integer recipientId) {
		log.info("删除预警接收人，ID: {}", recipientId);
		return recipientMapper.deleteById(recipientId);
	}

	@Override
	public List<WaringRecipient> getAllRecipientList(RecipientQueryDTO recipientQueryDTO) {
		// 不进行分页，查询所有符合条件的数据
		return recipientMapper.selectAll(recipientQueryDTO);

	}

	@Override
	public int batchInsertRecipient(List<WaringRecipient> recipientList) {
		log.info("批量添加预警接收人，共{}条数据", recipientList.size());
		return recipientMapper.batchInsert(recipientList);
	}

	@Override
	public int importRecipientData(MultipartFile file) {
		log.info("导入预警接收人数据，文件名: {}", file.getOriginalFilename());
		List<WaringRecipient> recipientList = new ArrayList<>();
		try {
			// 使用EasyExcel读取Excel文件
			EasyExcel.read(file.getInputStream(), WaringRecipient.class, new AnalysisEventListener<WaringRecipient>() {
				@Override
				public void invoke(WaringRecipient recipient, AnalysisContext context) {
					// 处理每一条数据
					log.debug("读取到接收人数据: {}", recipient);
					// 生成默认头像URL（如果没有）
					if (recipient.getRecipientAvatarUrl() == null || recipient.getRecipientAvatarUrl().isEmpty()) {
						// 这里可以设置一个默认头像URL
					}
					recipientList.add(recipient);
				}

				@Override
				public void doAfterAllAnalysed(AnalysisContext context) {
					log.info("Excel文件解析完成，共读取{}条数据", recipientList.size());
				}
			}).sheet().doRead();

			// 批量插入数据
			if (!recipientList.isEmpty()) {
				return batchInsertRecipient(recipientList);
			}
			return 0;
		} catch (Exception e) {
			log.error("导入Excel文件失败", e);
			throw new RuntimeException("导入Excel文件失败: " + e.getMessage());
		}
	}

}